package submarines;

//import SubmarinesBindings.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.*;
import submarines.notices.*;
import submarines.ui.*;
import java.util.*;
import SubmarinesBindings.*;
import java.io.FileOutputStream;
/**
 *
 * @author elad
 */
public class Main {
    
    static Ui userInterface;
    static public PlayerManager pManager;
    static NotficationManager nManager;
    GameManager game;
    quitAction act;
    
    static public enum quitAction {
       START_NEW_GAME,
       RESTART_GAME,
       QUIT_GAME;
       //INVALID_CHOICE;
       
        @Override
           public String toString() {
               String s;
               switch(this) {
                   case START_NEW_GAME: s = "Start new game"; break;
                   case RESTART_GAME: s = "Restart current match"; break;
                   case QUIT_GAME: s = "Quit the game"; break;
                   default: s= "Invalid option";
               }
                return s;
            };
        }
     
    public Main(Ui u) {
        userInterface = u;
        String xml = userInterface.askUserYesNoQuestions("Do you wnat to use XML file to start the game? (yes/no)");
        if ("yes".equals(xml))
        {
            try {
                testParseEntireDocument();
            } catch (JAXBException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            pManager  = new PlayerManager();
        }
            game  = new GameManager();
            nManager = new NotficationManager(pManager);

            game.playGame();
            act = Main.userInterface.getNewGameAswer();
            
            while (act !=quitAction.QUIT_GAME)
            {
                if (act == quitAction.START_NEW_GAME)
                {
                    pManager = new PlayerManager();
                    nManager = new NotficationManager(pManager);
                    game = new GameManager();
                    game.playGame();
                }
                if (act == quitAction.RESTART_GAME)
                {
                    pManager.reInitiate();
                    game.playGame();
                }
                act = Main.userInterface.getNewGameAswer();
            }
    }
    
    public Main(boolean xml) { //use for web
        //userInterface = u;
        //String xml = userInterface.askUserYesNoQuestions("Do you wnat to use XML file to start the game? (yes/no)");
        if (xml == true)
        {
            try {
                testParseEntireDocument();
            } catch (JAXBException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            pManager  = new PlayerManager();
        }
            game  = new GameManager();
            nManager = new NotficationManager(pManager);

            game.playGame();
            act = Main.userInterface.getNewGameAswer();
            
            while (act !=quitAction.QUIT_GAME)
            {
                if (act == quitAction.START_NEW_GAME)
                {
                    pManager = new PlayerManager();
                    nManager = new NotficationManager(pManager);
                    game = new GameManager();
                    game.playGame();
                }
                if (act == quitAction.RESTART_GAME)
                {
                    pManager.reInitiate();
                    game.playGame();
                }
                act = Main.userInterface.getNewGameAswer();
            }
    }
    
    public final void testParseEntireDocument() throws JAXBException {
        String path = Main.userInterface.askUserYesNoQuestions("Please enter the XML file path: ");
        JAXBContext jc = JAXBContext.newInstance(Boards.class.getPackage().getName()); 
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        SubmarinesBindings.Submarines sub = new SubmarinesBindings.Submarines();
        sub = (SubmarinesBindings.Submarines) unmarshaller.unmarshal(new File(path));
        List<SubmarinesBindings.Player> player = (List<SubmarinesBindings.Player>) sub.getPlayers().getPlayer();
        List<SubmarinesBindings.Boat> boats = (List<SubmarinesBindings.Boat>) sub.getBoats().getBoat();
        List<SubmarinesBindings.Board> boards = (List<SubmarinesBindings.Board>) sub.getBoards().getBoard();
        
        pManager = new PlayerManager(player, boats, boards);

    }
    
    public final void testParseEntireDocument(String path) throws JAXBException { //use for web
        //String path = Main.userInterface.askUserYesNoQuestions("Please enter the XML file path: ");
        JAXBContext jc = JAXBContext.newInstance(Boards.class.getPackage().getName()); 
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        SubmarinesBindings.Submarines sub = new SubmarinesBindings.Submarines();
        sub = (SubmarinesBindings.Submarines) unmarshaller.unmarshal(new File(path));
        List<SubmarinesBindings.Player> player = (List<SubmarinesBindings.Player>) sub.getPlayers().getPlayer();
        List<SubmarinesBindings.Boat> boats = (List<SubmarinesBindings.Boat>) sub.getBoats().getBoat();
        List<SubmarinesBindings.Board> boards = (List<SubmarinesBindings.Board>) sub.getBoards().getBoard();
        
        pManager = new PlayerManager(player, boats, boards);

    }
}
